package org.jeecg.modules.api.wlhy.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.api.wlhy.app.utils.HttpUtils;
import org.jeecg.modules.third.sendsms.util.HttpClientUtil;
import org.jeecg.modules.wlhy.app.entity.AppVersion;
import org.jeecg.modules.wlhy.app.service.IAppVersionService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @Description: app版本控制
 * @Author: jeecg-boot
 * @Date: 2020-12-23
 * @Version: V1.0
 */
@Api(tags = "移动端消息货源消息推送")
@RestController
@RequestMapping("/MessagePush")
@Slf4j
public class messagePushController extends JeecgController<AppVersion, IAppVersionService> {
    @Autowired
    private IAppVersionService appVersionService;

    @Autowired
    private IHyDriverService hyDriverService;

    /**
     *
     *
     * @param
     * @return
     */
    @ApiOperation(value = "getOpenidByCode")
    @GetMapping(value = "/getOpenidByCode")
    public Result<?> updateDriverOpenid(@RequestParam(name = "driverId") String driverId,
                                        @RequestParam(name = "js_code") String js_code) {
        if (StringUtils.isEmpty(driverId)) {
            return Result.error("司机信息错误");
        }
        if (StringUtils.isEmpty(js_code)) {
            return Result.error("js_code信息错误");
        }
        HyDriver driver = hyDriverService.getById(driverId);
        if (driver == null) {
            return Result.error("司机信息错误");
        }
        HyConfig systemConfig = HyConfigUtils.getConfig();
        String appid = systemConfig.getErweimaAppid();
        String secret = systemConfig.getErweimaSecret();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + js_code;
        String ret = HttpClientUtil.doGet(url);
        JSONObject jsonObject = JSON.parseObject(ret);
        System.out.println("ret:" + jsonObject);


//        hyDriverService.updateById(driver);

        return Result.OK();
    }


}
